<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>增加用户信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script type="text/javascript" src="../assets/js/jquery-1.8.3.min.js"></script>
    <script src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <div class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>姓名</label>
                <div class="layui-input-inline">
                    <input type="text" id="uname" maxlength="5" autocomplete="off"
                           class="layui-input"></div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red"></span></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>手机</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" maxlength="11" id="uphone"
                           class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>性别
                </label>
                <div class="layui-input-inline layui-show-xs-block">
                    <select name="contrller" id="usex">
                        <option value="0">请选择</option>
                        <option value="1">男</option>
                        <option value="2">女</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>年龄</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" maxlength="3" id="uage"
                           class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="username" id="pawor"
                           class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>确认密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="username" id="pawors"
                           class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="add" id="butt" lay-submit="">增加</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script type="text/javascript" src="../assets/js/jquery-1.8.3.min.js"></script>
<script>
    layui.use(['table', 'jquery','layer'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        var layer=layui.layer;

    $("#butt").click(function () {
        let uname = $("#uname").val();
        let uphone = $("#uphone").val();
        let usex = $("#usex option:selected").text();
        let uage = $("#uage").val();
        let pawor = $("#pawor").val();
        let pawors = $("#pawors").val();
        let flag = true;
        if (uname == "" || uphone == "" || usex == "" || uage == "" || pawor == "" || pawors == "" ) {
            layer.msg('注册信息填写完整!', {
                icon: 2, time: 800
            });
            flag = false;
        }
        var phone = /^1[3|4|5|8][0-9]\d{4,8}$/;
        if (flag) {
            if (!phone.test(uphone)) {
                layer.msg('请输入正确的手机号码!', {
                    icon: 2, time: 800
                });
                flag = false;
            }
        }
        if (flag) {
            if (usex != "男" & usex != "女") {
                layer.msg('性别输入有误!', {
                    icon: 2, time: 800
                });
                flag = false;
            }
        }
        var r = /^\+?[1-9][0-9]*$/;　　//判断是否为正整数
        if (flag) {
            if (!r.test(uage)) {
                layer.msg('请输入正确的年龄!', {
                    icon: 2, time: 800
                });
                flag = false;
            }
        }
        if (flag) {

                $.ajax({
                    type: "POST",
                    url: "http://localhost:8080/api/userinsert",
                    data: {
                        "uName": uname,
                        "uPhone": uphone,
                        "uSex": usex,
                        "uAge": uage,
                        "uPwd": pawor,
                    },
                    headers: {
                        token: localStorage.getItem("token")
                    },
                    datatype: "json",
                    success: function (data) {
                        if (data.phone == 0) {
                            layer.msg('此电话号码在用户信息没有找到!',{
                                icon:2
                                ,time:1000
                            });
                        } else if (data.phone == 1) {
                            layer.msg('此电话号码在用户账号信息已经注册过!',{
                                icon:2
                                ,time:1000
                            });
                        }else{
                            var index = parent.layer.getFrameIndex(window.name);
                            layer.msg('业主信息增加成功!',{
                                icon:1
                                ,time:1000
                            },function (){
                                parent.layer.close(index);//关闭当前页
                            });
                        }
                    }
                })

        }
    })
    })
</script>
